package com.ktjiaoyu.news.dao.impl;

import com.ktjiaoyu.news.dao.BaseDao;
import com.ktjiaoyu.news.dao.GoodsDao;
import com.ktjiaoyu.news.entity.Goods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodsDaolmpl extends BaseDao implements GoodsDao {
    @Override
    public List<Goods> getuserInfoPage(String mane, Integer pageNo, Integer pageSize) {
        List<Goods> list = new ArrayList<Goods>();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn = null;
        String sql = "select * from `news_goods` where 1 = 1 ";
        if (mane != null && mane.length() > 0) {
            sql += " AND mane like '%" + mane.trim() + "%'";
        }
        sql += " order by id  limit ?,?";
        rs = super.executeQuery(sql, (pageNo - 1) * pageSize, pageSize);
        try {
            while (rs.next()) {
                Goods goods = new Goods(
                        rs.getInt(1),
                        rs.getInt(2),
                        rs.getString(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getInt(6),
                        rs.getInt(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10),
                        rs.getString(11));
                list.add(goods);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(conn, pstmt, rs);
        }
        return list;
    }

    @Override
    public int count(String title) {
        return 0;
    }

    @Override
    public List<Goods> getAllGoodsList() {
        List<Goods> list = new ArrayList<Goods>();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn = null;
        String sql = "select * from news_goods";
        rs = super.executeQuery(sql);
        try {
            while (rs.next()) {
                Goods b = new Goods(
                        rs.getInt(1),
                        rs.getInt(2),
                        rs.getString(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getInt(6),
                        rs.getInt(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10),
                        rs.getString(11));
                list.add(b);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(conn, pstmt, rs);
        }
        return list;
    }

    @Override
    public int insert(Goods goods) {
        String sql = "insert into `news_goods`(`goodType`,`mane`,`age`,`price`,`inventory`,`category`,`describe`,`address`,`listing`,`removal`,) VALUES (?,?,?,?,?,?,?,?,?,?)\n";
        return super.executeUpdate(sql, goods.getGoodType(),
                goods.getMane(), goods.getAge(), goods.getPrice(),
                goods.getInventory(), goods.getCategory(),
                goods.getDescribe(), goods.getAddress(), goods.getListing(),
                goods.getRemoval());
    }

    @Override
    public Goods findById(int id) {
        Goods goods = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn = null;
        String sql = "select * from news_goods where id = ? ";
        rs = super.executeQuery(sql, id);
        try {
            if (rs.next()) {
                goods  = new Goods(
                        rs.getInt(1),
                        rs.getInt(2),
                        rs.getString(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getInt(6),
                        rs.getInt(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10),
                        rs.getString(11));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll(conn, pstmt, rs);
        }
        return goods;
    }

    @Override
    public int update(Goods goods) {
        String sql = "update `news_goods` set `goodType` = ?,`mane` = ?,`age` = ?,`price` = ?,`inventory` = ?,`category` = ?,`describe` = ?,`address` = ?,`listing` = ?,`removal` = ? where id = ?";
        return super.executeUpdate(sql, goods.getGoodType(),
                goods.getMane(), goods.getAge(), goods.getPrice(),
                goods.getInventory(), goods.getCategory(),
                goods.getDescribe(), goods.getAddress(), goods.getListing(),
                goods.getRemoval(), goods.getId());
    }

    @Override
    public int delById(int id) {
        String sql = "delete from `news_goods` where id = ?";
        return super.executeUpdate(sql, id);
    }
}
